package io.renren.modules.simida.service.impl;

import io.renren.common.rpc.OutrestSimidaFeignRpc;
import io.renren.common.utils.Constant;
import io.renren.common.utils.StringUtil;
import io.renren.datasource.annotation.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.simida.dao.UserFundLogDao;
import io.renren.modules.simida.entity.UserFundLogEntity;
import io.renren.modules.simida.service.UserFundLogService;


@Service("userFundLogService")
public class UserFundLogServiceImpl extends ServiceImpl<UserFundLogDao, UserFundLogEntity> implements UserFundLogService {
    @Autowired
    private OutrestSimidaFeignRpc outrestSimidaFeignRpc;
    @Override
    @DataSource(Constant.DB_SLAVE_SIMIDA)
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<UserFundLogEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.likeRight(StringUtil.isNotEmpty(params.get("tradeNumber")),"trade_number",params.get("tradeNumber"));
        queryWrapper.eq(StringUtil.isNotEmpty(params.get("payState")),"pay_state",params.get("payState"));
        queryWrapper.eq(StringUtil.isNotEmpty(params.get("payType")),"pay_type",params.get("payType"));
        queryWrapper.eq(StringUtil.isNotEmpty(params.get("modeType")),"mode_type",params.get("modeType"));
        boolean needTime = StringUtil.isNotEmpty(params.get("beginTime")) && StringUtil.isNotEmpty(params.get("endTime"));
        queryWrapper.between(needTime, "create_time", params.get("beginTime"), params.get("endTime"));
        queryWrapper.eq("state",1);
        queryWrapper.orderByDesc("create_time");
        IPage<UserFundLogEntity> page = this.page(
                new Query<UserFundLogEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    @DataSource(Constant.DB_SLAVE_SIMIDA)
    public UserFundLogEntity queryById(Long id) {
        return super.getById(id);
    }

    @Override
    public void compensatePay(List<String> tradeNos) {
        for (String tradeNo : tradeNos) {
            outrestSimidaFeignRpc.compensatePay(tradeNo);
        }
    }

    @Override
    @DataSource(Constant.DB_SLAVE_SIMIDA)
    public List<UserFundLogEntity> queryByCondition(Map<String, Object> params) {
        QueryWrapper<UserFundLogEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq(StringUtil.isNotEmpty(params.get("payState")),"pay_state",params.get("payState"));
        queryWrapper.eq(StringUtil.isNotEmpty(params.get("state")),"state",params.get("state"));
        return  super.list(queryWrapper);
    }

}